<template>
	<view class="news">
		<view class="news-list" v-for="item in datalist" :key="item.id" @click="goContent(item)">
			<view class="news-list__pic">
				<image mode="aspectFill" class="news-list__pic-img" v-if="item.pic" :src="formatPic(item.pic, 'orj360')"></image>
			</view>
			<view class="news-list__info">
				<view class="news-list__title">{{ item.title }}</view>
				<view class="news-list__time">{{ item.addtime }}</view>
			</view>
		</view>
		<Loading :status="loading ? 'loading' : finished ? 'noMore' : ''" v-if="loading || finished" />
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	import Loading from '@/components/Loading'
	import {
		formatPic
	} from '@/utils/index'
	export default {
		data() {
			return {
				loading: false,
				finished: false,
				datalist: [],
				page: 1
			}
		},
		methods: {
			...mapActions(['getNewsList']),
			formatPic,
			getData(more) {
				if (this.loading && !this.finished) return
				this.loading = true
				this.page = more ? this.page + 1 : 1
				this.getNewsList({
					id: 44,
					p: this.page
				}).then(res => {
					this.loading = false
					this.datalist = more ? [...this.datalist, ...res.data] : res.data
					if (res.data.length < 10) {
						this.finished = true
					}
				})
			},
			goContent(item) {
				uni.navigateTo({
					url: `/pages/article/index?id=${item.id}`
				})
			}
		},
		onLoad() {
			this.getData()
		},
		onReachBottom() {
			this.getData(1)
		},
		components: {
			Loading
		}
	}
</script>
<style lang="scss" scoped>
	@import '../../static/scss/mixin.scss';

	.news {
		padding-top: 20rpx;

		&-list {
			display: flex;
			margin-bottom: 20rpx;
			padding: 0 20rpx 20rpx;
			border-bottom: 1px solid #ebebeb;

			&__pic {
				width: 200rpx;
				height: 112rpx;
				overflow: hidden;

				&-img {
					display: initial;
					width: 100%;
					height: auto;
				}
			}

			&__info {
				width: 540rpx;
				margin-left: 20rpx;
			}

			&__title {
				font-size: 28rpx;
				@include clamp(2);
			}

			&__time {
				font-size: 24rpx;
				color: #999;
			}
		}
	}
</style>
